// package com.xuyuan.cpn.redis;
//
// import com.xuyuan.entity.MyTestEntity;
// import org.junit.jupiter.api.Test;
// import org.junit.runner.RunWith;
// import org.springframework.boot.test.context.SpringBootTest;
// import org.springframework.data.redis.core.*;
// import org.springframework.test.context.junit4.SpringRunner;
//
// import javax.annotation.Resource;
// import java.time.Duration;
// import java.util.*;
// import java.util.concurrent.TimeUnit;
//
// @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
// @RunWith(SpringRunner.class)
// public class RedisDemo {
//     @Resource
//     RedisTemplate<String, Object> redisTemplate;
//
//     /**
//      * 测试String操作
//      */
//     @Test
//     public void testString() throws Exception {
//         ValueOperations<String, Object> stringOperations = redisTemplate.opsForValue();
//         stringOperations.set("test", "test", 3, TimeUnit.SECONDS);
//         stringOperations.set("test1", "test1", Duration.ofSeconds(3));
//
//         Map<String, Object> map = new HashMap<>();
//         map.put("test2", "test2");
//         map.put("test3", "test3");
//         stringOperations.multiSet(map);
//
//         MyTestEntity myTestEntity = new MyTestEntity();
//         myTestEntity.setName("testEntity");
//         stringOperations.set("testEntity", myTestEntity);
//
//         System.out.println(stringOperations.get("test"));
//         System.out.println(stringOperations.get("test1"));
//         System.out.println(stringOperations.multiGet(Arrays.asList("test2", "test3")));
//         System.out.println(stringOperations.get("testEntity"));
//         Thread.sleep(5000);
//         System.out.println(stringOperations.get("test"));
//         System.out.println(stringOperations.get("test1"));
//     }
//
//     /**
//      * 测试Hash操作
//      */
//     @Test
//     public void testHash() throws Exception {
//         HashOperations<String, Object, Object> hashOperations = redisTemplate.opsForHash();
//         hashOperations.put("hash1", "hashKey1", "hashValue1");
//         hashOperations.put("hash1", "hashKey2", "hashValue2");
//         Map<String, Object> map = new HashMap<>();
//         map.put("hashKey3", "hashValue3");
//         map.put("hashKey4", "hashValue4");
//         hashOperations.putAll("hash2", map);
//         System.out.println(hashOperations.get("hash1", "hashKey1"));
//         System.out.println(hashOperations.get("hash2", "hashKey3"));
//     }
//
//     /**
//      * 测试队列操作
//      */
//     @Test
//     public void testList() {
//         ListOperations<String, Object> listOperations = redisTemplate.opsForList();
//         listOperations.leftPush("list1", "listValue1");
//         listOperations.leftPush("list1", "listValue2");
//         listOperations.leftPush("list1", "listValue3");
//         listOperations.leftPush("list1", "listValue4");
//         listOperations.leftPushAll("list1", "listValue5", "listValue6", "listValue7");
//
//         System.out.println(listOperations.range("list1", 0, -1));
//         System.out.println(listOperations.range("list1", 0, 2));
//
//         listOperations.leftPushAll("list2", "listValue1", "listValue2", "listValue3", "listValue4", "listValue5", "listValue6");
//         // 保留第3到第5个元素
//         listOperations.trim("list2", 3, 5);
//         System.out.println(listOperations.range("list2", 0, -1));
//
//     }
//
//     /**
//      * 测试集合操作
//      */
//     @Test
//     public void testSet() {
//         SetOperations<String, Object> setOperations = redisTemplate.opsForSet();
//         setOperations.add("set1", "setValue1", "setValue2", "setValue3", "setValue4", "setValue5", "setValue6");
//         setOperations.add("set2", "setValue2", "setValue3", "setValue4", "setValue5", "setValue6", "setValue7", "setValue8");
//         // 交集
//         System.out.println(setOperations.intersect("set1", "set2"));
//         // 并集
//         System.out.println(setOperations.union("set1", "set2"));
//         // set1的差集
//         System.out.println(setOperations.difference("set1", "set2"));
//         // set2的差集
//         System.out.println(setOperations.difference("set2", "set1"));
//         // pop任意移除某一元素返回
//         System.out.println(setOperations.pop("set1"));
//         System.out.println(setOperations.members("set1"));
//         // randomMember随机返回一个元素
//         System.out.println(setOperations.randomMember("set1"));
//         System.out.println(setOperations.members("set1"));
//         // 移除指定元素
//         System.out.println(setOperations.remove("set1", "setValue1"));
//         System.out.println(setOperations.members("set1"));
//     }
//
//     /**
//      * 测试有序集合操作：有score可以做排行榜
//      */
//     @Test
//     public void testZSet() {
//         ZSetOperations<String, Object> zSetOperations = redisTemplate.opsForZSet();
//         zSetOperations.add("zset1", "member1", 1.0);
//         zSetOperations.add("zset1", "member2", 2.0);
//         zSetOperations.add("zset1", "member3", 3.0);
//
//         // 批量添加成员到有序集合
//         Set<ZSetOperations.TypedTuple<Object>> membersWithScores = new HashSet<>();
//         membersWithScores.add(new DefaultTypedTuple<>("member4", 4.0));
//         membersWithScores.add(new DefaultTypedTuple<>("member5", 5.0));
//         zSetOperations.add("zset1", membersWithScores);
//
//         // 获取有序集合中指定排名范围内的成员（按分数升序）
//         Set<Object> range = zSetOperations.range("zset1", 0, -1);
//         System.out.println("Range of zset1 (ascending): " + range);
//
//         // 获取有序集合中指定排名范围内的成员（按分数降序）
//         Set<Object> reverseRange = zSetOperations.reverseRange("zset1", 0, -1);
//         System.out.println("Reverse range of zset1 (descending): " + reverseRange);
//
//         // 获取有序集合中分数在指定范围内的成员（按分数升序）
//         Set<Object> rangeByScore = zSetOperations.rangeByScore("zset1", 2.0, 4.0);
//         System.out.println("Range by score (2.0 to 4.0): " + rangeByScore);
//
//         // 获取有序集合中成员的排名（按分数升序）
//         Long rank = zSetOperations.rank("zset1", "member3");
//         System.out.println("Rank of 'member3': " + rank);
//
//         // 获取有序集合中成员的排名（按分数降序）
//         Long reverseRank = zSetOperations.reverseRank("zset1", "member3");
//         System.out.println("Reverse rank of 'member3': " + reverseRank);
//     }
//
// }
